// FILE: Chip8ConsoleDisplay.cpp by Richard Hughes 01/05/2012
//
// DESC: Defines the Win32 console chip 8 screen

// INCLUDES
#include <string>
#include <iostream>
#include <stdlib.h>
#include "Chip8ConsoleDisplay.h"

// NAMESPACES
using namespace std;

/*************************************************************/
//
// CLASS: Chip8ConsoleDisplay
//
// DESC: Defines the Win32 based console Chip 8 Display
//
/*************************************************************/

// sets up the display
// takes the handle to the console window
// returns nothing
void Chip8ConsoleDisplay::Init(HANDLE consoleHandle)
{
	this->_consoleHandle = consoleHandle;

	SMALL_RECT windowSize = {0, 0, this->_width + 15, this->_height + 30}; // 15 for debugger info
	SetConsoleWindowInfo(this->_consoleHandle, TRUE, &windowSize);

	SetConsoleTitle(L"Chip 8 Emulator - Richard Hughes");
} // Init

// clears the screen
// takes nothing
// returns nothing
void Chip8ConsoleDisplay::ClearScreen()
{
	Chip8Screen::ClearScreen();

	system("cls"); // the easy way
} // end ClearScreen

// renders the screen
// takes nothing
// returns nothing
void Chip8ConsoleDisplay::UpdateScreen()
{
	COORD pos;

	for(int y = 0; y < this->_height; y++)
	{
		string line;

		for(int x = 0; x < this->_width; x++)
		{
			if(this->GetPixelValue(x, y))
				line += '@';
			else
				line += ' ';
		}

		pos.X = -this->_xOffset;
		pos.Y = y - this->_yOffset;

		SetConsoleCursorPosition(this->_consoleHandle, pos);

		cout << line;
	}
} // end UpdateScreen

// EOF: Chip8ConsoleDisplay.cpp